package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.ReplyComment;
import cn.wolfcode.trip.base.mapper.QuestionReplyMapper;
import cn.wolfcode.trip.base.mapper.ReplyCommentMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IReplyCommentService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ReplyCommentServiceImpl implements IReplyCommentService {
    @Autowired
    private ReplyCommentMapper replyCommentMapper;

    @Autowired
    private QuestionReplyMapper questionReplyMapper;

    @Override
    public void saveOrUpdate(ReplyComment replyComment) {
        if (replyComment.getId() != null) {
            // 评论更新操作
            replyCommentMapper.updateByPrimaryKey(replyComment);
        } else {
            //设置当前时间为评论生成时间
            replyComment.setCommentTime(new Date());
            //设置当前用户为评论人
            replyComment.setCommentUser(UserContext.getCurrentUser());
            // 评论新增操作
            replyCommentMapper.insert(replyComment);
            //评论新增后,回答对应的评论数+1
            questionReplyMapper.updateCommentCountByPrimaryKey(replyComment.getReply().getId());
        }
    }

    @Override
    public PageInfo<ReplyComment> listById(ReplyComment rc, QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        //获取评论对应的回答
        List<ReplyComment> list = replyCommentMapper.selectAll(rc);
        return new PageInfo<>(list);
    }

    @Override
    public ReplyComment getById(Long id) {
        return replyCommentMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<ReplyComment> listByUserId(ReplyComment rc, QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<ReplyComment> list = replyCommentMapper.listByUserId(rc);
        return new PageInfo<>(list);
    }


}
